﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyFirstWindowsFormsApplication
{
    public partial class Form1 : Form
    {
        PrinterUserControl CourentPrinter;
        Queue<PrinterUserControl> pqueue = new Queue<PrinterUserControl>();
 

        public Form1()
        {
            
            InitializeComponent();
            //this.printButton.Click+=printButton_Click;      
            
              foreach (Control item in tableLayoutPanel2.Controls)
                  	{
                        if (item is PrinterUserControl)
                        {
                            PrinterUserControl printer = item as PrinterUserControl;
                            pqueue.Enqueue(printer);
                            

                        }
                          }
              addCourentPrinter();
	}

        private void addCourentPrinter()
        {
            CourentPrinter = pqueue.Dequeue();
            CourentPrinter.InkEmpty += CourentPrinter_EventErr;
            CourentPrinter.PageMissing += CourentPrinter_EventErr;
        }

        void CourentPrinter_EventErr(object sender, EventArgs e)
        {
            PrinterUserControl pr = sender as PrinterUserControl;
            PrinterEventArgs er = e as PrinterEventArgs;
            MessageBox.Show(er.msg, pr.Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            
            pqueue.Enqueue(CourentPrinter);
            CourentPrinter.InkEmpty -= CourentPrinter_EventErr;
            CourentPrinter.PageMissing -= CourentPrinter_EventErr;
            addCourentPrinter();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void print()
        {

            if (true)
            {
                Random rand = new Random();
                int InkCount = rand.Next(10, 30);
                int PageCount = rand.Next(15, 30);
                if (CourentPrinter.PageCount >= PageCount && CourentPrinter.InkCount>15)
                {
                    //CourentPrinter.print(PageCount, InkCount);
                }
                if (CourentPrinter.PageCount >= PageCount || CourentPrinter.InkCount < 15)
                {
                    if (CourentPrinter.PageCount >= PageCount){
                        PrinterEventArgs er = new PrinterEventArgs { Critical = true, Date = DateTime.Now, msg = "Missing" + (CourentPrinter.PageCount - PageCount).ToString() + "pages" };
                        er.Critical = true;
                       //CourentPrinter.PageMissing(this,new PrinterEventArgs{Critical=true ,Date=};
                }
                }
             
            }
        }


        private void printButton_Click(object sender, EventArgs e)
        {
            CourentPrinter.print();
        }
    }
    
}
